/*
    网络请求
*/
import { Host } from '../conf.js';

import { ChanInfo,ChanAccess } from '../service';
import { SetToken } from './token.js';

export const Post = (path, data = "") => fetch(Host + path, {
    method: "POST",
    body: typeof data === "string" ? data : JSON.stringify(data),
    headers: {
            "Content-Type": "application/json",
            "x-token": localStorage.getItem("admin-token"),
    },
}).catch(err => {
    console.log(err)
    ChanInfo.set({status: false,msg: "net error"});
}).then(async res => {
    // console.log(res.url=='http://eva7base.com:89/admin/notice')
    if(!res) return null
    SetToken(res.headers.get("x-token"))
    let ret = await res.json();
    if(ret.Code === 2) {
        // 跳转页面
        ChanAccess.set(false);
        return
    }
    // console.log(path)
    if ((path!=='/admin/notice')){
        ChanInfo.set({status: ret.Code === 0,msg: ret.Msg});
    }
    if(!ret.Data && !ret.Code) {
        return "";
    }
    return ret.Data;
});
